<template>
  <div class="app-container org">
    <el-card>
      <el-row>
        <el-col :span="24">
          <p class="pinformation">搜索条件</p>
        </el-col>
      </el-row>
      <el-form ref="queryRef" label-width="100" :model="queryParams">
        <el-row>
          <el-col :span="5">
            <el-form-item label="标准物质名称：" prop="bzwzName" label-width="">
              <el-input clearable maxlength="100" placeholder="请输入" v-model="queryParams.bzwzName"
                @keyup.enter="handleQuery" />
            </el-form-item>
          </el-col>
          <el-col :span="2">
            <el-form-item label="种类：" prop="bzwzType" label-width="80">
              <el-select placeholder="请选择" clearable v-model="queryParams.bzwzType" filterable>
                <el-option v-for="dict in proxy.addListNoLimit(sys_bzwz_type)" :key="dict.value" :label="dict.label"
                  :value="parseInt(dict.value)" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="3">
            <el-form-item label="唯一性编号：" prop="bzwzCode" label-width="">
              <el-input clearable maxlength="100" placeholder="请输入唯一性编号" v-model="queryParams.bzwzCode"
                @keyup.enter="handleQuery" />
            </el-form-item>
          </el-col>
          <el-col :span="3">
            <el-form-item label="批次号：" prop="produceCode" label-width="80">
              <el-input clearable maxlength="100" placeholder="请输入批次号" v-model="queryParams.produceCode"
                @keyup.enter="handleQuery" />
            </el-form-item>
          </el-col>
          <!-- <el-col :span="4">
            <el-form-item label="检测项目：" prop="bucketId" label-width="80">
              <el-select v-model="queryParams.bucketId" filterable clearable>
                <el-option
                  v-for="item in proxy.addListNoLimit(
                    jcxmList,
                    'bucketId',
                    'jcxmName'
                  )"
                  :key="item.bucketId"
                  :value="item.bucketId"
                  :label="item.jcxmName"
                >
                  <el-tooltip
                    class="box-item"
                    effect="customized"
                    placement="bottom"
                  >
                    <template #content>
                      <span v-html="transformation(item.jcxmName)"></span>
                    </template>
                    <div
                      style="
                        white-space: nowrap;
                        overflow: hidden;
                        text-overflow: ellipsis;
                      "
                    >
                      <span v-html="transformation(item.jcxmName)"></span>
                    </div>
                  </el-tooltip>
                </el-option>
              </el-select>
            </el-form-item>
          </el-col> -->
          <el-col :span="4">
            <el-form-item label="检测项目：" prop="bucketId" label-width="80">
              <el-select v-model="queryParams.bucketId" clearable filterable remote reserve-keyword remote-show-suffix
                :remote-method="remoteMethod" :loading="loading" style="width: 100%">
                <el-option v-for="item in jcxmList" :key="item.bucketId" :value="item.bucketId" :label="item.jcxmName">
                  <span v-html="transformation(item.jcxmName)"></span>
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="检测项目类型：" prop="isSingle" label-width="">
              <el-select placeholder="请选择" clearable v-model="queryParams.isSingle" filterable>
                <el-option v-for="dict in proxy.addListNoLimit(sys_bzwz_jcxm_type)" :key="dict.value" :label="dict.label"
                  :value="parseInt(dict.value)" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="3">
            <el-form-item label="归属类别：" prop="gsType" label-width="80">
              <el-select placeholder="请选择" clearable v-model="queryParams.gsType" filterable>
                <el-option v-for="dict in proxy.addListNoLimit(sys_bzwz_gslb)" :key="dict.value" :label="dict.label"
                  :value="parseInt(dict.value)" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="5">
            <el-form-item label="入库日期：" prop="expirationType">
              <el-date-picker v-model="dateRange" value-format="YYYY-MM-DD" type="daterange" :clearable="false"
                :editable="false" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="生产厂商：" prop="produceName" label-width="80">
              <el-input clearable maxlength="100" placeholder="请输入生产厂商" v-model="queryParams.produceName"
                @keyup.enter="handleQuery" />
            </el-form-item>
          </el-col>
          <el-col :span="3">
            <el-form-item label="定值单位：" prop="dzUnit" label-width="80">
              <el-input clearable maxlength="100" placeholder="请输入生产厂商" v-model="queryParams.dzUnit"
                @keyup.enter="handleQuery" />
            </el-form-item>
          </el-col>
          <el-col :span="3">
            <el-form-item label="保存位置：" prop="saveLocation" label-width="80">
              <el-input clearable maxlength="100" placeholder="请输入保存位置" v-model="queryParams.saveLocation"
                @keyup.enter="handleQuery" />
            </el-form-item>
          </el-col>
          <el-col :span="3">
            <el-row justify="center">
              <el-radio label="0" v-model="queryParams.isLike">模糊搜索</el-radio>
              <el-radio label="1" v-model="queryParams.isLike">精准搜索</el-radio>
            </el-row>
          </el-col>
          <el-col :span="3">
            <el-button type="success" @click="handleQuery">搜索</el-button>
            <el-button class="resetBtn" @click="resetQuery">重置</el-button>
          </el-col>
        </el-row>
      </el-form>
      <el-row>
        <p class="pinformation">在库标准物质列表</p>
        <p class="totalStyle">共 {{ total }} 条数据</p>
        <el-button type="success" link icon="DocumentDelete" style="margin-left: 10px; padding-bottom: 5px"
          @click="handleExport">导出台账</el-button>
        <!-- <el-button type="success" link icon="DocumentDelete"
          style="margin-left: 10px; padding-bottom: 5px">标准物质一览表</el-button> -->
        <el-table ref="mainTableRef" :data="bzwzTable">
          <el-table-column label="唯一性编号" prop="bzwzCode" align="center" />
          <el-table-column label="标准物质名称" prop="bzwzName" align="center" width="230" />
          <el-table-column label="种类" prop="bzwzType" align="center">
            <template #default="scope">
              <dict-tag :options="sys_bzwz_type" :value="scope.row.bzwzType" />
            </template>
          </el-table-column>
          <el-table-column label="生产厂商" prop="produceName" align="center" />
          <el-table-column label="批次号" prop="produceCode" align="center" />
          <el-table-column label="入库日期" prop="createDate" align="center" />
          <el-table-column label="有效日期" prop="yxDate" align="center" />
          <el-table-column label="保存位置" prop="saveLocation" align="center" />
          <el-table-column label="保存方式" prop="saveCondition" align="center" />
          <el-table-column label="规格" prop="totalGg" align="center">
            <template #default="scope">
              {{ scope.row.totalGg }}
              <span v-html="transformation(scope.row.ggUnitName)
                "></span>
            </template>
          </el-table-column>
          <el-table-column label="余量" prop="remainGg" align="center">
            <template #default="scope">
              <el-button link icon="EditPen" @click="remainGgClick(scope.row)">{{ scope.row.remainGg }}</el-button>
            </template>
          </el-table-column>
          <el-table-column label="检测项目数" prop="jcxmCount" align="center">
            <template #default="scope">
              <el-button link @click="jcxmCountClick(scope.row.bzwzId)">{{
                scope.row.jcxmName ? scope.row.jcxmName : scope.row.jcxmCount
              }}</el-button>
            </template>
          </el-table-column>
          <el-table-column label="溶剂" prop="rj" align="center" />
          <el-table-column label="单价" prop="price" align="center" />
          <el-table-column label="代理厂商" prop="agentName" align="center" />
          <el-table-column label="证书位置" prop="certificateLocation" align="center" />
          <el-table-column label="出厂编号" prop="factoryCode" align="center" />
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template #default="scope">
              <el-button link type="success" @click="xqBzwz(scope.row.bzwzId)">详情</el-button>
            </template>
          </el-table-column>
        </el-table>
        <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
          v-model:limit="queryParams.pageSize" @pagination="getList" />
      </el-row>
    </el-card>
    <!-- 点击详情弹窗 -->
    <el-dialog :class="'el-dialog-theme3'" append-to-body draggable :close-on-click-modal="false"
      v-model="open" :title="xqTitle" @close="closedXqDialog">
      <el-form label-width="100" ref="xqRef" v-model="form">
        <el-row>
          <el-col :span="8">
            <el-form-item label="标准物质名称：" prop="bzwzName">
              {{ form.bzwzName || "/" }}
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="唯一性编号：" prop="bzwzCode">
              {{ form.bzwzCode }}
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="种类：" prop="bzwzType">
              <dict-tag :options="sys_bzwz_type" :value="form.bzwzType" v-if="form.bzwzType === 0 || form.bzwzType" />
              <span v-else>/</span>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="出厂编号：" prop="factoryCode">
              {{ form.factoryCode || "/" }}
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="生产批号：" prop="produceCode">
              {{ form.produceCode || "/" }}
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="定值单位：" prop="dzUnit">
              {{ form.dzUnit || "/" }}
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="入库日期：" prop="createDate">
              {{ form.createDate || "/" }}
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="有效期：" prop="yxDate">
              {{ form.yxDate || "/" }}
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="溶剂：" prop="rj">
              {{ form.rj || "/" }}
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="规格：">
              <span v-html="transformation(form.totalGg + form.ggUnitName)" v-if="form.totalGg"></span>
              <span v-else>/</span>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="保存方式：" prop="saveCondition">
              {{ form.saveCondition || "/" }}
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="归属类别：" prop="gsType">
              <dict-tag :options="sys_bzwz_gslb" :value="form.gsType" v-if="form.gsType === 0 || form.gsType" />
              <span v-else>/</span>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="单价（元）：" prop="price">
              {{ form.price || "/" }}
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="生产厂商：" prop="produceName">
              {{ form.produceName || "/" }}
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="代理厂商：" prop="agentName">
              {{ form.agentName || "/" }}
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="入库人：" prop="bzwzAdminId">
              {{
                proxy.getItemByArray(
                  userList,
                  "userId",
                  form.bzwzAdminId,
                  "nickName"
                ) || "/"
              }}
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="证书位置：" prop="">
              {{ form.certificateLocation || "/" }}
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="保存位置：" prop="saveLocation">
              {{ form.saveLocation || "/" }}
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col>
            <el-form-item label="开封后有效期：">
              <span v-html="transformation(form.yxPeriod + form.yxUnitName)
                " v-if="form.yxPeriod"></span>
              <span v-else>/</span>
            </el-form-item>
          </el-col>
        </el-row>
        <p class="pinformation">证书附件</p>
        <fileUpload v-model="form.fileList" :isDisplay="false" v-if="form.fileList.length"></fileUpload>
        <span v-if="!form.fileList.length" style="margin-left: 10px">/</span>
        <el-row style="margin-top: 10px">
          <p class="pinformation">检测项目列表</p>
          <p class="totalStyle">共 {{ form.bzwzJcxmList.length }} 条数据</p>
        </el-row>
        <el-table :data="form.bzwzJcxmList">
          <el-table-column label="检测项目" prop="bucketId" align="center">
            <template #default="scope">
              <span v-html="transformation(scope.row.jcxmName)"></span>
            </template>
          </el-table-column>
          <el-table-column label="浓度值" prop="con" align="center" />
          <el-table-column label="浓度值单位" prop="conUnit" align="center">
            <template #default="scope">
              <span v-html="transformation(scope.row.conUnitName)"></span>
            </template>
          </el-table-column>
          <el-table-column label="不确定度值" prop="notvalue" align="center" />
          <el-table-column label="不确定度单位" align="center">
            <template #default="scope">
              <span v-html="transformation(scope.row.subNotvalueName)"></span>
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="danger" @click="open = false">关 闭</el-button>
        </div>
      </template>
    </el-dialog>
    <!-- 点击行内检测项目数的弹窗 -->
    <el-dialog :class="'el-dialog-theme3'" append-to-body draggable :close-on-click-modal="false"
      v-model="jcxmOpen" @close="closedDialog" :title="title">
      <el-row>
        <p class="pinformation">检测项目列表</p>
        <p class="totalStyle">共 {{ jcxmTable.length }} 条数据</p>
      </el-row>
      <el-table :data="jcxmTable">
        <el-table-column label="检测项目" prop="jcxmName" align="center">
          <template #default="scope">
            <span v-html="transformation(scope.row.jcxmName)"></span>
          </template>
        </el-table-column>
        <el-table-column label="浓度值" prop="con" align="center" />
        <el-table-column label="浓度值单位" prop="conUnit" align="center">
          <template #default="scope">
            <span v-html="transformation(scope.row.conUnitName)"></span>
          </template>
        </el-table-column>
        <el-table-column label="不确定度值" prop="notvalue" align="center" />
        <el-table-column label="不确定度单位" prop="subNotvalueName" align="center">
          <template #default="scope">
            <span v-html="transformation(scope.row.subNotvalueName)"></span>
          </template>
        </el-table-column>
      </el-table>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="danger" @click="jcxmOpen = false">关 闭</el-button>
        </div>
      </template>
    </el-dialog>
    <el-dialog :class="'el-dialog-theme3'" append-to-body draggable :close-on-click-modal="false"
      v-model="remainGgOpen" :title="remainGgTitle" @close="closedRemainGgDialog">
      <el-form :model="remainGgForm">
        <el-form-item label="余量：" prop="remainGg">
          <el-input-number placeholder="请输入余量" :min="0" style="width: 20%" :max="10000" controls-position="right"
            v-model="remainGgForm.remainGg" />
        </el-form-item>
        <el-form-item label="说明：" prop="remark">
          <el-input clearable type="textarea" placeholder="请输入说明" :autosize="{ minRows: 3, maxRows: 7 }"
            v-model="remainGgForm.remark" maxlength="500" show-word-limit />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="success" @click="remainGgSave">保 存</el-button>
          <el-button type="danger" @click="remainGgOpen = false">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="BzwzLedger">
import {
  newListLedger,
  bzwzList,
  BzwzViewForm,
  getJcxmList,
  saveRemainGg,
  getRemainGg,
} from "@/api/resource/ledger";
import { listUser } from "@/api/contract/check";
import { getUnitList } from "@/api/store/unit";

const { proxy } = getCurrentInstance();
const { sys_bzwz_type, sys_bzwz_gslb, sys_bzwz_gqlx, sys_bzwz_jcxm_type } =
  proxy.useDict(
    "sys_bzwz_type",
    "sys_bzwz_gslb",
    "sys_bzwz_gqlx",
    "sys_bzwz_jcxm_type"
  );

const dateRange = ref([]);
const bzwzTable = ref([]);
const jcxmTable = ref([]);
const remainGgOpen = ref(false);
const remainGgTitle = ref("编辑余量");
const title = ref("查看检测项目");
const jcxmOpen = ref(false);
const total = ref(0);
const open = ref(false);
const xqTitle = ref("查看标准物质详情");
const remainGgForm = ref({});
const data = reactive({
  form: { bzwzJcxmList: [], fileList: [] },
  queryParams: {
    pageNum: 1, // 当前页数
    pageSize: 10, // 当前页数据条数
    isLike: "0",
    bzwzType: -1,
    isSingle: -1,
    gsType: -1,
  },
  rules: {
    // packageName: [{ required: true, message: "包名不能为空", trigger: "blur" }],
    // jclbId: [{ required: true, message: "检测类别不能为空", trigger: "blur" }]
  },
});

const { queryParams, form, rules } = toRefs(data);

const heightParams = ref([{ tableName: "main", bottomHeight: 21 }]);

const unitList = ref([]);
getUnitList({ unitGroup: 0 }).then((res) => {
  unitList.value = res.data;
});

const yxUnitList = ref([]);
getUnitList({ unitGroup: 4 }).then((res) => {
  yxUnitList.value = res.data;
});

function getTableHeight() {
  proxy.adaptiveHeight(proxy, heightParams.value);
}
onMounted(() => {
  nextTick(() => {
    getTableHeight();
    window.addEventListener("resize", getTableHeight);
  })
})
onUpdated(() => {
  nextTick(function () {
    getTableHeight();
  });
});
onBeforeUnmount(() => {
  window.removeEventListener("resize", getTableHeight);
});

function getList() {
  newListLedger(proxy.addDateRange(queryParams.value, dateRange.value)).then(
    (res) => {
      bzwzTable.value = res.rows;
      total.value = res.total;
    }
  );
}
getList();

function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

function resetQuery() {
  dateRange.value = [];
  queryParams.value.isLike = "0";
  proxy.resetForm("queryRef");
  handleQuery();
}

function jcxmCountClick(row) {
  bzwzList({ bzwzId: row }).then((res) => {
    jcxmTable.value = res.data;
    jcxmOpen.value = true;
  });
}

function closedDialog() {
  jcxmTable.value = [];
}

function xqBzwz(bzwzId) {
  BzwzViewForm(bzwzId).then((res) => {
    form.value = res.data;
    open.value = true;
  });
}

function closedXqDialog() {
  form.value = {
    bzwzJcxmList: [],
    fileList: [],
  };
}

function closedRemainGgDialog() {
  remainGgForm.value = {};
}

watch(
  () => total.value,
  () => {
    if (total.value) {
      heightParams.value = [{ tableName: "main", bottomHeight: 73 }];
    } else {
      heightParams.value = [{ tableName: "main", bottomHeight: 21 }];
    }
    getTableHeight();
  }
);

const jcxmList = ref([]);
getJcxmList().then((res) => {
  jcxmList.value = res.data;
});

function transformation(row) {
  if (row) {
    const a = proxy.getSupAndSub(row);
    return a;
  }
}

function remainGgClick(row) {
  getRemainGg(row.bzwzId).then((res) => {
    remainGgForm.value = res.data;
    remainGgOpen.value = true;
  });
}

const userList = ref([]);
listUser().then((res) => {
  userList.value = res.data;
});

function remainGgSave() {
  saveRemainGg(remainGgForm.value).then((res) => {
    remainGgOpen.value = false;
    proxy.$modal.msgSuccess("保存成功");
    getList();
  });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    "/resource/bzwz/export",
    {
      ...queryParams.value,
    },
    `标准物质入库登记表_${new Date().getTime()}.xlsx`
  );
}

const loading = ref(false);

function remoteMethod(val) {
  if (val) {
    loading.value = true;
    setTimeout(() => {
      getJcxmList({ jcxmName: val }).then((res) => {
        loading.value = false;
        jcxmList.value = res.rows;
      });
    }, 200);
  } else {
    jcxmList.value = [];
  }
}
</script>

<style lang="scss" scoped>
.el-radio {
  margin-right: 5px;
}
</style>
